package com.yanqu.road.server.manger.activity.xiayue;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.xiayue.config.XiaYueAnimalConfig;
import com.yanqu.road.entity.activity.xiayue.config.XiaYueConfig;
import com.yanqu.road.entity.activity.xiayue.config.XiaYueNpcConfig;
import com.yanqu.road.entity.activity.xiayue.config.XiaYuePlaceConfig;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.activity.XiaYueBusiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.xiayue.XiaYueModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.RandomHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class XiaYueMgr extends TempMgr {

    private static XiaYueConfig xiaYueConfig;

    public static XiaYueConfig getXiaYueConfig() {
        return xiaYueConfig;
    }

    public static ActivityInfo getActivityInfo() {
        return xiaYueConfig == null ? null : xiaYueConfig.getActivityInfo();
    }

    @Override
    public boolean reload() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void reloadActivityData() {
        getLogger().info("reload XiaYueActivity start");

        // 获取进行中的活动信息
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.XiaYueActivity.getValue());
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no XiaYueActivity in show time");
            return;
        }

        XiaYueConfig tmpConfig = new XiaYueConfig();
        tmpConfig.setActivityInfo(openActivityInfoList.get(0));
        int tmpActivityId = tmpConfig.getActivityInfo().getActivityId();

        tmpConfig.setPlaceConfigMap(XiaYueBusiness.getXiaYuePlaceConfigMap(tmpActivityId));
        tmpConfig.setAnimalConfigMap(XiaYueBusiness.getXiaYueAnimalConfigMap(tmpActivityId));
        tmpConfig.setCombConfigMap(XiaYueBusiness.getXiaYueCombConfigMap(tmpActivityId));
        tmpConfig.setNpcConfigList(XiaYueBusiness.getXiaYueNpcConfigList(tmpActivityId));

        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityId);
        if(activityConfigMap.containsKey(eActivityConfigType.XIAYUE_SCORE_ITEM_ID.getName())) {
            tmpConfig.setScoreItemId(activityConfigMap.get(eActivityConfigType.XIAYUE_SCORE_ITEM_ID.getName()).getIntValue());
        }else if(activityConfigMap.containsKey(eActivityConfigType.EXCHANGE_SCORE_ITEM_ID.getName())){
            tmpConfig.setScoreItemId(activityConfigMap.get(eActivityConfigType.EXCHANGE_SCORE_ITEM_ID.getName()).getIntValue());
        }
        tmpConfig.setSearchItemId(activityConfigMap.get(eActivityConfigType.XIAYUE_SEARCH_ITEM_ID.getName()).getIntValue());
        tmpConfig.setGuaranteeQuality(activityConfigMap.get(eActivityConfigType.XIAYUE_GUARANTEE_QUALITY.getName()).getIntValue());
        List<Integer> guaranteeTimesList = activityConfigMap.get(eActivityConfigType.XIAYUE_GUARANTEE_TIMES_PARAM.getName()).getIntListValue(";");
        tmpConfig.setNoGuaranteeTimes(guaranteeTimesList.get(0));
        tmpConfig.setGuaranteeTimes(guaranteeTimesList.get(1));
        tmpConfig.setMaidenNpcId(activityConfigMap.get(eActivityConfigType.XIAYUE_MAIDEN_NPC_ID.getName()).getIntValue());
        tmpConfig.setInitPlaceId(activityConfigMap.get(eActivityConfigType.XIAYUE_INIT_PLACE.getName()).getIntValue());
        tmpConfig.setCostEnergyList(activityConfigMap.get(eActivityConfigType.XIAYUE_COST_ENERGY_PARAM.getName()).getIntListValue(";"));
        xiaYueConfig = tmpConfig;

        getLogger().info("reload XiaYueActivity finish");

        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            XiaYueModule xiaYueModule = player.getModule(XiaYueModule.class);
            xiaYueModule.syncConfig();
            xiaYueModule.initUserData();
            xiaYueModule.syncData();
        }

    }

    public static boolean inClearSearchItemPeriod() {
        if (xiaYueConfig == null) return true;
        if (ActivityMgr.activityInRewardPeriod(xiaYueConfig.getActivityInfo()) ||
                !ActivityMgr.activityInShowTime(xiaYueConfig.getActivityInfo()))
            return true;
        return false;
    }

    /**
     * 抽地点
     */
    public static XiaYuePlaceConfig searchPlace(int commonTimes, Random random) {
        int type = calSearchType(commonTimes);

        // 找地点
        List<Integer> placeIdList = new ArrayList<>();
        List<Integer> placeWeightList = new ArrayList<>();
        for (XiaYuePlaceConfig placeConfig : xiaYueConfig.getPlaceConfigMap().values()) {
            boolean haveGuarantee = false;
            boolean haveCommon = false;
            for (Integer animalId : placeConfig.getAnimalList()) {
                XiaYueAnimalConfig animalConfig = xiaYueConfig.getAnimalConfigMap().get(animalId);
                if (animalConfig != null) {
                    if (animalConfig.getQuality() == xiaYueConfig.getGuaranteeQuality()) haveGuarantee = true;
                    else haveCommon = true;
                }
                if (haveGuarantee && haveCommon) break;
            }
            // 根据类型找符合要求的地点
            boolean canChose = false;
            if (type == 0 && !placeConfig.getAnimalList().isEmpty())
                canChose = true; // 0正常抽
            else if (type == 1 && haveCommon)
                canChose = true; // 1只能抽普通的
            else if (type == 2 && haveGuarantee)
                canChose = true; // 2只能抽保底
            if (canChose) {
                placeIdList.add(placeConfig.getId());
                placeWeightList.add(placeConfig.getWeight());
            }
        }

        // 抽地点
        int index = RandomHelper.getRandomIndexByWeight(placeWeightList, random);
        XiaYuePlaceConfig placeConfig = xiaYueConfig.getPlaceConfigMap().get(placeIdList.get(index));

        return placeConfig;
    }

    /**
     * 抽动物
     */
    public static XiaYueAnimalConfig searchAnimal(int placeId, int commonTimes, Random random) {
        int type = calSearchType(commonTimes);

        XiaYuePlaceConfig placeConfig = xiaYueConfig.getPlaceConfigMap().get(placeId);

        // 找动物
        List<XiaYueAnimalConfig> animalConfigList = new ArrayList<>();
        List<Integer> animalWeightList = new ArrayList<>();
        for (int i = 0; i < placeConfig.getAnimalList().size(); i++) {
            XiaYueAnimalConfig animalConfig = xiaYueConfig.getAnimalConfigMap().get(placeConfig.getAnimalList().get(i));
            // 根据类型找符合要求的动物
            boolean canChose = false;
            if (type == 0)
                canChose = true; // 0正常抽
            else if (type == 1 && animalConfig.getQuality() != xiaYueConfig.getGuaranteeQuality())
                canChose = true; // 1只能抽普通的
            else if (type == 2 && animalConfig.getQuality() == xiaYueConfig.getGuaranteeQuality())
                canChose = true; // 2只能抽保底
            if (canChose) {
                animalConfigList.add(animalConfig);
                animalWeightList.add(placeConfig.getWeightList().get(i));
            }
        }

        // 抽动物
        int index = RandomHelper.getRandomIndexByWeight(animalWeightList, random);
        XiaYueAnimalConfig animalConfig = animalConfigList.get(index);

        return animalConfig;
    }

    /**
     * 计算抽取类型
     *
     * @param commonTimes 未抽到保底的次数，不包含本次
     * @return 1抽不到保底，2必定保底，3正常概率
     */
    public static int calSearchType(int commonTimes) {
        int curTimes = commonTimes + 1;
        if (curTimes <= xiaYueConfig.getNoGuaranteeTimes())
            return 1;
        else if (curTimes > xiaYueConfig.getGuaranteeTimes())
            return 2;
        else
            return 0;
    }

    /**
     * 获取随机npcId，不包含巫女
     */
    public static int getRandomNpcId(Random random) {
        List<XiaYueNpcConfig> npcConfigList = xiaYueConfig.getNpcConfigList();
        int index = random.nextInt(npcConfigList.size());
        if (npcConfigList.get(index).getNpcId() == xiaYueConfig.getMaidenNpcId()) {
            index = (index + 1) % npcConfigList.size();
        }
        return npcConfigList.get(index).getNpcId();
    }
}
